package com.sojson.controller.po;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.HotelOrderParamVerify;
import com.sojson.entity.bo.OrderParam;
import com.sojson.entity.po.HotelOrder;
import com.sojson.entity.vo.HotelOrderVo;
import com.sojson.enums.EOrderStatusHotel;
import com.sojson.result.Result;
import com.sojson.service.HotelOrderService;
import com.sojson.util.ArithUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 入驻记录信息控制器
 * 
 * @author liu
 * @date 2021-07-02
 */
@Api(tags = "入驻记录信息管理")
@RestController
@RequestMapping("/hotelOrder")
public class HotelOrderController {

    @Autowired
    private HotelOrderService hotelOrderService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/insert')")
    @ApiOperation("给入驻记录信息添加一条数据")
    public Result<Integer> insert(@RequestBody HotelOrder dto) throws IOException {
        // 校验参数
        Result<Integer> paramResult = HotelOrderParamVerify.insert(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        HotelOrder hotelOrder = new HotelOrder();
        hotelOrder.setNoRoom(dto.getNoRoom());
        hotelOrder.setStartTime(dto.getStartTime());
        hotelOrder.setEndTime(dto.getEndTime());
        hotelOrder.setNosCoupon(dto.getNosCoupon());
        hotelOrder.insertInit();
        return Result.success(hotelOrderService.insert(hotelOrder));
    }

    /**
     * 批量插入
     * 
     * @param hotelOrder
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/insertList')")
    @ApiOperation("给入驻记录信息添加多条数据")
    public Result<Integer> insertList(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.insertList(hotelOrder));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param hotelOrder
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/deleteByDto')")
    @ApiOperation("通过对象批量删除入驻记录信息数据(物理删除)")
    public Result<Integer> deleteByDto(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.deleteByDto(hotelOrder));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/countAll')")
    @ApiOperation("统计入驻记录信息全部数据(包含逻辑删除的)")
    public Result<Integer> countAll() {
        return Result.success(hotelOrderService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/listAll')")
    @ApiOperation("查询入驻记录信息所有数据(包含逻辑删除的)")
    public Result<List<HotelOrder>> listAll() {
        return Result.success(hotelOrderService.listAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param hotelOrder
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/countByDto')")
    @ApiOperation("根据对象统计入驻记录信息数据")
    public Result<Integer> countByDto(HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.countByDto(hotelOrder));
    }

    /**
     * 根据对象查询
     * 
     * @param hotelOrder
     * @return
     */
    @GetMapping("/listByDto")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/listByDto')")
    @ApiOperation("根据对象查询入驻记录信息数据")
    public Result<List<HotelOrder>> listByDto(HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.listByDto(hotelOrder));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/deleteById/{id}')")
    @ApiOperation("通过id删除入驻记录信息数据(物理删除)")
    @ApiImplicitParam(name = "id", value = "id")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(hotelOrderService.deleteById(id));
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/deleteByIds')")
    @ApiOperation("通过id数组删除入驻记录信息数据(物理删除)")
    @ApiImplicitParam(name = "ids", value = "id数组")
    public Result<Integer> deleteByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(hotelOrderService.deleteByIds(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateAllById")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateAllById')")
    @ApiOperation("通过id更新入驻记录信息数据(全部更新)")
    public Result<Integer> updateAllById(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateAllById(hotelOrder));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateAllByIdList")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateAllByIdList')")
    @ApiOperation("通过id列表更新入驻记录信息数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByIdList(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateAllByIdList(hotelOrder));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateDtoListAllById")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateDtoListAllById')")
    @ApiOperation("通过id列表更新入驻记录信息数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllById(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateDtoListAllById(hotelOrder));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateById')")
    @ApiOperation("通过id更新入驻记录信息数据(局部更新)")
    public Result<Integer> updateById(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateById(hotelOrder));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateByIdList")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateByIdList')")
    @ApiOperation("通过id列表更新入驻记录信息数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByIdList(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateByIdList(hotelOrder));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateDtoListById")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateDtoListById')")
    @ApiOperation("通过id列表更新入驻记录信息数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListById(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateDtoListById(hotelOrder));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/findById/{id}')")
    @ApiOperation("通过id查询入驻记录信息数据")
    @ApiImplicitParam(name = "id", value = "id")
    public Result<HotelOrder> findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(hotelOrderService.findById(id));
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/findByIds/{ids}')")
    @ApiOperation("通过id数组查询入驻记录信息数据")
    @ApiImplicitParam(name = "ids", value = "id数组")
    public Result<List<HotelOrder>> findByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(hotelOrderService.findByIds(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param hotelOrder
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/findIdByDto')")
    @ApiOperation("通过对象查询入驻记录信息id列表")
    public Result<List<Integer>> findIdByDto(HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.findIdByDto(hotelOrder));
    }

    /**
     * 通过no删除(物理删除)
     * 
     * @param no
     * @return
     * @throws IOException 
     */
    @DeleteMapping("/deleteByNo/{no}")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/deleteByNo/{no}')")
    @ApiOperation("通过no删除入驻记录信息数据(物理删除)")
    @ApiImplicitParam(name = "no", value = "no")
    public Result<Integer> deleteByNo(@PathVariable String no) throws IOException {
        if (no == null) {
            return Result.success();
        }

        return Result.success(hotelOrderService.deleteByNo(no));
    }

    /**
     * 通过no数组删除(物理删除)
     * 
     * @param nos
     * @return
     */
    @DeleteMapping("/deleteByNos/{nos}")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/deleteByNos/{nos}')")
    @ApiOperation("通过no数组删除入驻记录信息数据(物理删除)")
    @ApiImplicitParam(name = "nos", value = "no数组")
    public Result<Integer> deleteByNos(@PathVariable String[] nos) {
        if (nos.length == 0) {
            return Result.success();
        }

        return Result.success(hotelOrderService.deleteByNos(nos));
    }

    /**
     * 通过no更新(全部更新)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateAllByNo")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateAllByNo')")
    @ApiOperation("通过no更新入驻记录信息数据(全部更新)")
    public Result<Integer> updateAllByNo(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateAllByNo(hotelOrder));
    }

    /**
     * 通过no列表更新(全部更新)(更新内容相同)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateAllByNoList")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateAllByNoList')")
    @ApiOperation("通过no列表更新入驻记录信息数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByNoList(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateAllByNoList(hotelOrder));
    }

    /**
     * 通过no列表更新(全部更新)(更新内容不相同)
     * 
     * @param hotelOrders
     * @return
     */
    @PutMapping("/updateDtoListAllByNo")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateDtoListAllByNo')")
    @ApiOperation("通过no列表更新入驻记录信息数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllByNo(@RequestBody HotelOrder hotelOrders) {
        return Result.success(hotelOrderService.updateDtoListAllByNo(hotelOrders));
    }

    /**
     * 通过no更新(局部更新)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateByNo")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateByNo')")
    @ApiOperation("通过no更新入驻记录信息数据(局部更新)")
    public Result<Integer> updateByNo(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateByNo(hotelOrder));
    }

    /**
     * 通过no列表更新(局部更新)(更新内容相同)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateByNoList")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateByNoList')")
    @ApiOperation("通过no列表更新入驻记录信息数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByNoList(@RequestBody HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.updateByNoList(hotelOrder));
    }

    /**
     * 通过no列表更新(局部更新)(更新内容不相同)
     * 
     * @param hotelOrders
     * @return
     */
    @PutMapping("/updateDtoListByNo")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateDtoListByNo')")
    @ApiOperation("通过no列表更新入驻记录信息数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListByNo(@RequestBody HotelOrder hotelOrders) {
        return Result.success(hotelOrderService.updateDtoListByNo(hotelOrders));
    }

    /**
     * 通过no查询
     * 
     * @param no
     * @return
     */
    @GetMapping("/findByNo/{no}")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/findByNo/{no}')")
    @ApiOperation("通过no查询入驻记录信息数据")
    @ApiImplicitParam(name = "no", value = "no")
    public Result<HotelOrder> findByNo(@PathVariable String no) {
        if (no == null) {
            return Result.success();
        }

        return Result.success(hotelOrderService.findByNo(no));
    }

    /**
     * 通过no数组查询
     * 
     * @param nos
     * @return
     */
    @GetMapping("/findByNos/{nos}")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/findByNos/{nos}')")
    @ApiOperation("通过no数组查询入驻记录信息数据")
    @ApiImplicitParam(name = "nos", value = "no数组")
    public Result<List<HotelOrder>> findByNos(@PathVariable String[] nos) {
        if (nos.length == 0) {
            return Result.success();
        }

        return Result.success(hotelOrderService.findByNos(nos));
    }

    /**
     * 通过对象查询no列表
     * 
     * @param hotelOrder
     * @return
     */
    @GetMapping("/findNoByDto")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/findNoByDto')")
    @ApiOperation("通过对象查询入驻记录信息no列表")
    public Result<List<String>> findNoByDto(HotelOrder hotelOrder) {
        return Result.success(hotelOrderService.findNoByDto(hotelOrder));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/clear')")
    @ApiOperation("清空入驻记录信息数据(物理清空)")
    public Result<Integer> clear() {
        return Result.success(hotelOrderService.clear());
    }

    /**
     * 根据对象查询
     * 
     * @param hotelOrder
     * @return
     * @throws IOException 
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/list')")
    @ApiOperation("根据对象查询入驻记录信息数据")
    public Result<List<HotelOrderVo>> list(HotelOrderVo hotelOrder) throws IOException {
        hotelOrder.setNoMerchant(TokenUtil.getUserNo());
        return Result.success(hotelOrderService.list(hotelOrder));
    }

    /**
     * 根据对象查询
     * 
     * @param hotelOrder
     * @return
     * @throws IOException 
     */
    @GetMapping("/listApp")
    @ApiOperation("根据对象查询入驻记录信息数据")
    public Result<List<HotelOrderVo>> listApp(HotelOrderVo hotelOrder) throws IOException {
        hotelOrder.setNoUser(TokenUtil.getUserNo());
        return Result.success(hotelOrderService.list(hotelOrder));
    }

    /**
     * 通过no查询
     * 
     * @param no
     * @return
     */
    @GetMapping("/getInfo/{no}")
    @ApiOperation("通过no查询入驻记录信息数据")
    @ApiImplicitParam(name = "no", value = "no")
    public Result<HotelOrder> getInfo(@PathVariable String no) {
        if (no == null) {
            return Result.success();
        }

        return Result.success(hotelOrderService.getInfo(no));
    }

    /**
     * 下单
     * 
     * @param orderParam
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/orders")
    @PreAuthorize("@ss.login()")
    public Object ordersBuy(@RequestBody OrderParam orderParam) throws Exception {
        // 校验参数
        Result<?> paramResult = HotelOrderParamVerify.orders(orderParam);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(hotelOrderService.orders(orderParam));
    }

    /**
     * 取消订单
     * 
     * @param no
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/cancel/{no}")
    @PreAuthorize("@ss.login()")
    @ApiOperation("取消酒店订单")
    public Result<Integer> cancel(@PathVariable String no) throws Exception {
        if (StringUtil.isBlankObject(no)) {
            return Result.fail("订单编号不能为空！");
        }

        return Result.success(hotelOrderService.cancel(no));
    }

    /**
     * 订单退款
     * 
     * @param hotelOrder
     * @return
     * @throws Exception 
     */
    @PutMapping("/refund")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/refund')")
    @ApiOperation("订单退款")
    public Result<Integer> refund(@RequestBody HotelOrder hotelOrder) throws Exception {
        BigDecimal price = hotelOrder.getPrice();
        if (StringUtil.isBlankObjectListOne(hotelOrder.getNo(), price)) {
            return Result.fail("缺少参数！");
        }
        int intValue = price.intValue();
        if (intValue < 1) {
            return Result.fail("退款金额不能小于0.01！");
        }

        HotelOrder ho = new HotelOrder();
        ho.setPrice(hotelOrder.getPrice());
        ho.setPrice(ArithUtil.intPennyToBigDollar(intValue));
        return Result.success(hotelOrderService.refund(ho));
    }

    /**
     * 通过no更新状态(局部更新)
     * 
     * @param hotelOrder
     * @return
     */
    @PutMapping("/updateStatus")
    @PreAuthorize("@ss.hasPermi('/hotelOrder/updateByNo')")
    @ApiOperation("通过no更新入驻记录信息数据(局部更新)")
    public Result<Integer> updateStatus(@RequestBody HotelOrder hotelOrder) {
        Integer status = hotelOrder.getStatus();
        if (StringUtil.isBlankObject(status)) {
            return Result.fail("状态不能为空");
        }
        if (status != EOrderStatusHotel.CHECK_YES.getCode() && status != EOrderStatusHotel.CLOSE_YES.getCode()) {
            return Result.fail("状态不是已入住或已关闭");
        }
        List<String> nos = hotelOrder.getNos();
        if (StringUtil.isBlankObject(nos) || nos.size() < 1) {
            return Result.fail("编号列表不能为空");
        }

        HotelOrder ho = new HotelOrder();
        ho.updateInit();
        ho.setNos(hotelOrder.getNos());
        ho.setStatus(hotelOrder.getStatus());
        return Result.success(hotelOrderService.updateStatus(ho));
    }

}